package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YCKOKO
 * Date: 2022-10-22
 * Time: 17:05
 */
public class DelOperation implements IOPeration{
    public void work(BookList bookList){
        //删除图书
        System.out.println("删除图书");
        System.out.println("请输入你要删除的图书名称：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int index = -1;
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())){
                //System.out.println("找到了你所要查找的图书");
                index = i;
                break;
            }
        }
        if(index == -1) {
            System.out.println("你所要删除的图书不存在");
            return;
        }

        for (int i = index; i < currentSize; i++) {
            Book book = bookList.getPos(i+1);
            bookList.setBooks(book,i);
        }

        //每次删除 都要置空
        bookList.setBooks(null,currentSize-1);

        bookList.setUsedSize(currentSize-1);
        System.out.println("删除图书成功！");
    }
}
